package com.base.helper;

import org.springframework.util.DigestUtils;

public class UserKeyUtils {

    /**
     *
     * @param t 上一个时间戳
     * @return
     */
    public static boolean isTimeOut(Long t) {
        long lt = System.currentTimeMillis();
        long dt = (lt - t) / (1000 * 60);
        return dt > 5 || dt < 0;
    }

    /**
     *
     * @param uk 本地计算的私密串
     * @param userkey 传过来的私密串
     * @return
     */
    public static boolean Verify(String uk, String userkey){
        return StringUtils.isEquals(uk, userkey);
    }

    /**
     *
     * @param teacherid
     * @param t
     * @param key config表中的密钥（value值）
     * @return
     */
    public static String encode(String teacherid, Long t, String key) {
        String s = "";
        try {
            String useKey = key + teacherid + t;
            s = DigestUtils.md5DigestAsHex(useKey.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }
}
